package com.zqxx.examine.portal.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.model.question.Comment;
import com.zqxx.examine.portal.persistence.CommentMapper;

@Service 
public class  CommentService
{
  @Autowired
  private CommentMapper commentMapper;
  
  public List<Comment> getCommentByTypeAndReferId(int commentType, int referId, int indexId, Page<Comment> page)
  {
    return this.commentMapper.getCommentByTypeAndReferId(commentType, referId, indexId, page);
  }
  
  @Transactional
  public void addComment(Comment comment)
  {
    try
    {
      Object index = this.commentMapper.getMaxCommentIndexByTypeAndReferId(comment.getCommentType(), comment.getReferId());
      int i = 0;
      if (index == null) {
        i = 0;
      } else {
        i = ((Integer)index).intValue();
      }
      comment.setIndexId(i + 1);
      this.commentMapper.addComment(comment);
    }
    catch (Exception e)
    {
      throw new RuntimeException(e);
    }
  }
}
